package com.liaoban.app.ui.mine;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.api.type.User;
import com.liaoban.app.api.type.request.StatusSwitchRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.helper.ImageHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.MainActivity;
import com.liaoban.app.ui.base.BaseFragment;
import com.liaoban.app.util.CommonUtils;
import com.liaoban.app.widget.MyAlertDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/7/9 0009.
 * 我的
 */
public class MineFragment extends BaseFragment {
    @BindView(R.id.iv_status)
    ImageView ivStatus;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_fans)
    TextView tvFans;
    @BindView(R.id.tv_liao_money)
    TextView tvLiaoMoney;
    @BindView(R.id.layout_recharge)
    LinearLayout layoutRecharge; //充值
    @BindView(R.id.layout_withdraw)
    LinearLayout layoutWithdraw; //提现
    @BindView(R.id.view_line1)
    View viewLine1;
    @BindView(R.id.layout_bill)
    LinearLayout layoutBill;
    @BindView(R.id.view_line2)
    View viewLine2;
    @BindView(R.id.layout_earn)
    LinearLayout layoutEarn;
    @BindView(R.id.view_line3)
    View viewLine3;
    @BindView(R.id.layout_album)
    LinearLayout layoutAlbum;
    @BindView(R.id.view_line4)
    View viewLine4;
    @BindView(R.id.layout_video)
    LinearLayout layoutVideo;
    @BindView(R.id.tv_anchor)
    TextView tvAnchor;
    @BindView(R.id.tv_registration_id)
    TextView tv_registration_id;
    @BindView(R.id.iv_grade)
    ImageView ivGrade;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View contentView) {
        tv_registration_id.setText("registration id:" + CommonUtils.getRegistrationID(getActivity()));
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!CommonBiz.getInstance().isLogin(getActivity())) {
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.changeTab(0);
        }
        showRunningDialog();
        startTask(CommonBiz.getInstance().userinfo(), userBaseResponse -> {
            setView(userBaseResponse.data);
        });
    }

    private void setView(User data) {
        String id = data.getWuserId() + "";
        String name = data.getWuserName();
        String src = data.getWuserSrc();
        String fansCount = data.getWuserNum() + "";
        String lineStatus = data.getLineStatus() + "";
        String wuserStatus = ConfigHelper.getString(getActivity(), ConfigKey.USER_WUSER_STATUS);
        String liaoMoney = data.getYu_money() + "";
        String wuserGrade = data.getWuserGrade();
        ImageHelper.load(ivAvatar, CommonUtils.getFullPic(src));
        tvName.setText(name);
        tvFans.setText(fansCount);
        tvId.setText(id);
        if ("1".equals(lineStatus)) {
            ivStatus.setSelected(false);
        } else if ("2".equals(lineStatus)) {
            ivStatus.setSelected(true);
        } else if ("3".equals(lineStatus)) {
            ivStatus.setSelected(true);
        }
        if ("1".equals(wuserStatus)) { //是主播
            layoutRecharge.setVisibility(View.GONE);
            layoutWithdraw.setVisibility(View.VISIBLE);
            viewLine1.setVisibility(View.GONE);
            layoutBill.setVisibility(View.GONE);
            viewLine2.setVisibility(View.VISIBLE);
            layoutEarn.setVisibility(View.VISIBLE);
            viewLine3.setVisibility(View.VISIBLE);
            layoutAlbum.setVisibility(View.VISIBLE);
            viewLine4.setVisibility(View.VISIBLE);
            layoutVideo.setVisibility(View.VISIBLE);
            tvAnchor.setVisibility(View.GONE);
        } else { //普通用户
            layoutRecharge.setVisibility(View.VISIBLE);
            layoutWithdraw.setVisibility(View.GONE);
            viewLine1.setVisibility(View.VISIBLE);
            layoutBill.setVisibility(View.VISIBLE);
            viewLine2.setVisibility(View.GONE);
            layoutEarn.setVisibility(View.GONE);
            viewLine3.setVisibility(View.GONE);
            layoutAlbum.setVisibility(View.GONE);
            viewLine4.setVisibility(View.GONE);
            layoutVideo.setVisibility(View.GONE);
            tvAnchor.setVisibility(View.VISIBLE);
        }
        tvLiaoMoney.setText(liaoMoney);
        ivGrade.setImageResource(CommonUtils.getLargeGradeRes(wuserGrade));
        ConfigHelper.save(getActivity(), ConfigKey.USER_SRC, src);
        ConfigHelper.save(getActivity(), ConfigKey.USER_LINE_STATUS, lineStatus);
        ConfigHelper.save(getActivity(), ConfigKey.USER_LIAO_MONEY, liaoMoney);
        ConfigHelper.save(getActivity(), ConfigKey.USER_GRADE, wuserGrade);
    }

    @OnClick(R.id.iv_status)
    public void status() {
        StatusSwitchRequest statusSwitchRequest = new StatusSwitchRequest();
        showRunningDialog();
        if (ivStatus.isSelected()) {
            statusSwitchRequest.setLineStatus("1");
            startTask(CommonBiz.getInstance().statusSwitch(statusSwitchRequest), baseResponse -> {
                ConfigHelper.save(getActivity(), ConfigKey.USER_LINE_STATUS, "1");
                ivStatus.setSelected(false);
            });
        } else {
            statusSwitchRequest.setLineStatus("2");
            startTask(CommonBiz.getInstance().statusSwitch(statusSwitchRequest), baseResponse -> {
                ConfigHelper.save(getActivity(), ConfigKey.USER_LINE_STATUS, "2");
                ivStatus.setSelected(true);
            });
        }
    }

    @OnClick({R.id.iv_set, R.id.iv_right, R.id.iv_avatar, R.id.tv_fans, R.id.layout_recharge, R.id.layout_bill, R.id.layout_earn, R.id.layout_album, R.id.layout_video, R.id.layout_share_money, R.id.tv_anchor, R.id.layout_black_list, R.id.layout_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_set:
                startActivity(new Intent(getActivity(), SetActivity.class));
                break;
            case R.id.iv_right:
            case R.id.iv_avatar:
                startActivity(new Intent(getActivity(), EditDataActivity.class));
                break;
            case R.id.tv_fans:
                startActivity(new Intent(getActivity(), FansActivity.class));
                break;
            case R.id.layout_recharge:
                startActivity(new Intent(getActivity(), RechargeActivity.class));
                break;
            case R.id.layout_withdraw:
                showRunningDialog();
                startTask(CommonBiz.getInstance().tixianStatus(), objectBaseResponse -> {
                    if (2 == objectBaseResponse.code) {
                        new MyAlertDialog(getActivity()).builder().setMsg(getString(R.string.un_bind_pay_tip)).
                                setPositiveButton("绑定支付宝", view1 -> startActivity(new Intent(getActivity(), BindPayActivity.class))).
                                setNegativeButton("", null).show();
                    } else if (0 == objectBaseResponse.code) {
                        ToastHelper.show(getActivity(), objectBaseResponse.msg);
                    } else {
                        startActivity(WithDrawActivity.newIntent(getActivity(), WithDrawActivity.TYPE_TIXIAN, objectBaseResponse.data.getKeyi_money() + "", objectBaseResponse.data.getAlipay_name()));
                    }
                });
                break;
            case R.id.layout_bill:
                startActivity(new Intent(getActivity(), EarnListActivity.class));
                break;
            case R.id.layout_earn:
                startActivity(new Intent(getActivity(), MyEarnActivity.class));
                break;
            case R.id.layout_album:
                startActivity(new Intent(getActivity(), AlbumActivity.class));
                break;
            case R.id.layout_video:
                startActivity(new Intent(getActivity(), MyVideoActivity.class));
                break;
            case R.id.layout_share_money:
                startActivity(new Intent(getActivity(), ShareMoneyActivity.class));
                break;
            case R.id.tv_anchor:
                showRunningDialog();
                startTask(CommonBiz.getInstance().isProve(), baseResponse -> {
                    if (baseResponse.code == 1) {
                        startActivity(new Intent(getActivity(), AnchorAgreementActivity.class));
                    } else {
                        ToastHelper.show(getActivity(), baseResponse.msg);
                    }
                });
                break;
            case R.id.layout_black_list:
                startActivity(new Intent(getActivity(), BlackListActivity.class));
                break;
        }
    }
}
